fix(sinopac): fetch contracts on demand when Stocks lookup misses#16
Open
koreal6803 wants to merge 1 commit into
Open
fix(sinopac): fetch contracts on demand when Stocks lookup misses#16koreal6803 wants to merge 1 commit into
koreal6803 wants to merge 1 commit into
Conversation
On shioaji <1.7, login() uses fetch_contract=False so no contracts are
downloaded. _get_contract then hit `return stocks[stock_id]` on an empty
Contracts.Stocks, raising KeyError('Contract not found: <id>') for actively
traded stocks (e.g. 8421). The fetch_contracts() fallback was unreachable
because the empty Stocks object is not None.
Now the KeyError (or None) from the Stocks lookup falls through to
fetch_contracts() + retry, so <1.7 users can place orders; genuinely
delisted symbols still raise as before. shioaji 1.7 path (api.contracts.get)
is unchanged.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
問題
finlab 永豐 broker 在 shioaji
<1.7環境下,用戶下單時對正常交易的股票(例:8421 旭源)回下單失敗: 'Contract not found: 8421'。根因
SinopacAccount登入用api.login(..., fetch_contract=False),shioaji <1.7 因此不會下載合約檔。_get_contract()走到:空的
Contracts.Stocks物件is not None為真,fetch_contracts()fallback 永遠不會被觸發。shioaji 1.7 走api.contracts.get()不受影響。修法
Stocks 查詢 miss(KeyError 或 None)時落到
fetch_contracts()+ 重試;真正下市的代碼仍照常 raise。測試
以 stub 模擬三種情境(不需真實 shioaji)驗證抽取自本檔的實際
_get_contract:api.contracts.get()直接解析 ✅嚴重度:中(影響所有 pin
shioaji<1.7且有實際下單的永豐用戶;升級 shioaji>=1.7 可繞過)。🤖 Generated with Claude Code